package manager

import (
	"BlazeRider-backend/app/models"
	"fmt"
	"sync"
)

// manager/user_mem_storage.go
type userMemStorage struct {
	users map[string]*UserInfo
	mu    sync.RWMutex
}

func newUserMemStorage() *userMemStorage {
	return &userMemStorage{
		users: make(map[string]*UserInfo),
	}
}

func (s *userMemStorage) InitializeUser(userID, userName, headImg string, level int) error {
	s.mu.Lock()
	defer s.mu.Unlock()

	if _, exists := s.users[userID]; !exists {
		s.users[userID] = &UserInfo{
			UserID:   userID,
			UserName: userName,
			HeadImg:  headImg,
			Level:    level,
			Score:    0,
		}
	}
	return nil
}

// user_mem_storage.go补全方法
func (s *userMemStorage) GetUser(userID string) (*UserInfo, error) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	user, exists := s.users[userID]
	if !exists {
		return nil, fmt.Errorf("can not get user")
	}
	return user, nil
}

func (s *userMemStorage) RecordScore(userID string, score int) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	if user, exists := s.users[userID]; exists {
		user.Score += score
	}
	return nil
}

func (s *userMemStorage) SetUserScoreByLiveRoomResult() error {
	return fmt.Errorf("setUserScoreByLiveRoomResult no mem logic")
}
func (s *userMemStorage) SetUserLevelByLevelSet(levelSetArr map[int]LevelSet) error {
	return fmt.Errorf("setUserLevelByLevelSet no mem logic")
}

func (um *userMemStorage) GetRanks() (*models.RankInfos, error) {

	return nil, fmt.Errorf("GetRanks no mem logic")
}

func (um *userMemStorage) GetWorldRecords() (*models.WorldRecods, error) {

	return nil, fmt.Errorf("GetRanks no mem logic")
}
